{% extends "base.html" %}
{% block title %}
    <title>出版社列表</title>
{% endblock %}

{% block content %}
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                <div>
                    <div class="row">
                        <div class="col-sm-12">
                            <h1 class="m-0">
                                出版社模块
                                <small>列表</small>
                            </h1>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--内容开始-->
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12 search-collapse">
                        <form id="search_form" method="get">
{#                            {% form.csrf_token %}#}
                            <div class="form-group row">
                                <label for="inputtext" class="col-form-label">出版社名称：</label>
                                <div class="col-md-4">
<!--                                    <input type="text" id="search_name" name="name" class="form-control"/>-->
                                    {{form.name(value=request.args.get('name',''))}}
                                </div>
                                <div class="col-md-4">
                                    <button type="submit" class="btn btn-info"><i class="fa fa-plus"></i>查询</button>
                                    <button type="reset" class="btn btn-secondary" id="resetBtn"><i class="fa fa-refresh"></i>重置</button>
                                    <a class="btn btn-primary single" href="{{url_for('pub_add')}}">
                                        <i class="fa fa-plus"></i> 新增
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </section>

        <div class="col-sm-12 main">
            <br>
            <div class="panel panel-primary">
                <div class="panel-body">
                    <table class="table table-bordered table-condensed table-striped table-hover">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>出版社名称</th>
                            <th>出版社地址</th>
                            <th>功能操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for per in pubs %}
                            <tr>
                                <td>{{ per.p_id }}</td>
                                <td>{{ per.name }}</td>
                                <td>{{ per.address }}</td>
                                <td width="20%">
                                    <a class="btn btn-primary single" href="{{url_for('pub_edit',p_id=per.p_id)}}">
                                        <i class="fa fa-edit"></i> 修改
                                    </a>
                                    <a class="btn btn-danger" href="javascript:void(0)" onclick="showDeleteModal(this)">删除</a>
                                    <input type="hidden" id="id_hidden" value={{ per.p_id }}>
                                </td>
                            </tr>
                        {% else %}
                            <tr>
                                <td colspan="7">无相关记录！</td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                     <nav aria-label="Contacts Page Navigation">
            <ul class="pagination justify-content-center m-2">
                {% if pagination.has_prev %}
                    <li class="page-item">
                        <a class="page-link"
                           href="{{ url_for('pub_list') }}?page={{ pagination.prev_num }}&name={{ name }}">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                {% endif %}

                {% for pg in pagination.iter_pages() %}
                    {% if   pg == pagination.page %}
                        <li class="page-item active">
                            <a class="page-link" href="">{{ pg }}</a>
                        </li>
                    {% else %}
                        <li class="page-item">
                        <a class="page-link"
                           href="{{ url_for('pub_list') }}?page={{ pg }}&name={{ name }}">{{ pg }}</a>
                    {% endif %}
                {% endfor %}


                {% if pagination.has_next %}
                    <li class="page-item">
                        <a class="page-link"
                           href="{{ url_for('pub_list') }}?page={{ pagination.next_num }}&name={{ name }}">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                {% endif %}
            </ul>
        </nav>
                </div>
            </div>
        </div>

<!--        <nav aria-label="Contacts Page Navigation">-->
<!--            <ul class="pagination justify-content-center m-2">-->
<!--                {% if pubs.has_previous %}-->
<!--                    <li class="page-item">-->
<!--                        <a class="page-link"-->
<!--                           href="{{ url_for('pub_list') }}?page={{ pubs.previous_page_number }}&name={{ name }}">-->
<!--                            <span aria-hidden="true">&laquo;</span>-->
<!--                        </a>-->
<!--                    </li>-->
<!--                {% endif %}-->

<!--{#                {% for pg in pubs.paginator.page_range %}#}-->
<!--{#                    {% if pubs.number == pg %}#}-->
<!--{#                        <li class="page-item active">#}-->
<!--{#                            <a class="page-link" href="">{{ pg }}</a>#}-->
<!--{#                        </li>#}-->
<!--{#                    {% else %}#}-->
<!--{#                        <li class="page-item">#}-->
<!--{#                            <a class="page-link"#}-->
<!--{#                               href="{{ url_for('pub_list') }}?page={{ pg }}&name={{ name }}</a>#}-->
<!--{#                        </li>#}-->
<!--{#                    {% endif %}#}-->
<!--{#                {% endfor %}#}-->


<!--                {% if pubs.has_next %}-->
<!--                    <li class="page-item">-->
<!--                        <a class="page-link"-->
<!--                           href="{{ url_for('pub_list') }}?page={{ pubs.next_page_number }}&name={{ name }}">-->
<!--                            <span aria-hidden="true">&raquo;</span>-->
<!--                        </a>-->
<!--                    </li>-->
<!--                {% endif %}-->
<!--            </ul>-->
<!--        </nav>-->
<!--    </div>-->

    <!-- 信息删除确认 -->
    <div class="modal fade" id="delModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" style="float:left">提示信息</h4>
                    <button type="button" class="close" data-dismiss="modal"
                            aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>

                </div>
                <div class="modal-body">
                    <p id="info">您确认要删除当前数据吗？</p>
                    <input type="hidden" id="del_id">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <a id="delButton" class="btn btn-success" data-dismiss="modal">确定</a>
                </div>
            </div>
        </div>
    </div>
    <script>
        // 打开模态框并设置需要删除的ID
        function showDeleteModal(obj) {
            var $tds = $(obj).parent().children();// 获取到删除元素的所在列
            var delete_id = $($tds[2]).val();// 获取隐藏控件的ID
            console.log(delete_id)
            $("#del_id").val(delete_id);// 给模态框中需要删除的ID赋值
            $("#delModal").modal({
                backdrop: 'static',
                keyboard: false
            });
        };

        $(function () {
            // 模态框的确定按钮的点击事件
            $("#delButton").click(function () {
                var id = $("#del_id").val();
                console.log("del" + id)
                // ajax异步删除
                $.ajax({
                    url: "/pub_del",
                    type: "POST",
                    data:{p_id:id},
                    success: function (result) {
                        if (result.code == "200") {
                            $("#delModal").modal("hide");
                            alert(result.message);
                            window.location.href = "{{ url_for('pub_list') }}";
                        }
                        else{
                            alert(result.message);
                        }
                    }
                })
            });
        });
    </script>
        <script>
        $(function () {
    // 重置按钮点击事件
    $("#resetBtn").click(function () {
        // 清空表单字段
        $("input[name='name']").val('');
        // 提交表单以刷新页面并显示所有数据
        $("#search_form").submit();
    });
    });
    </script>
{% endblock %}